<template>
  <div v-if="visible" class="duplicate-dialog-overlay" @click="handleClose">
    <div class="duplicate-dialog" @click.stop>
      <div class="duplicate-dialog-header">
        <h3>向量标注重复</h3>
      </div>
      <div class="duplicate-dialog-content">
        <div class="duplicate-warning">
          <p>⚠️ 向量组合与现有颜色完全相同，无法保存。请修改标注内容后重试。</p>
        </div>
        <div v-if="conflictColorInfo" class="duplicate-color-info">
          <h4>冲突的颜色信息：</h4>
          <div class="color-info-container">
            <div class="color-images">
              <h5>商品图片：</h5>
              <div class="conflict-image-grid">
                <div
                  v-for="image in getConflictImages(conflictColorInfo)"
                  :key="image.id"
                  class="conflict-image-item"
                >
                  <img
                    :src="getFullImageUrl(image.thumbnail_url || image.url)"
                    :alt="`角度 ${image.angle}`"
                    class="conflict-image"
                  />
                  <span class="conflict-image-angle">{{ getAngleDisplayName(image.angle) }}</span>
                </div>
              </div>
            </div>
            <div class="color-details">
              <div class="color-item">
                <span class="label">款式编码：</span>
                <span class="value">{{ getFieldValue(conflictColorInfo, 'style_code') }}</span>
              </div>
              <div class="color-item">
                <span class="label">颜色编码：</span>
                <span class="value">{{ getFieldValue(conflictColorInfo, 'color_code') }}</span>
              </div>
              <div class="color-item">
                <span class="label">颜色名称：</span>
                <span class="value">{{ getFieldValue(conflictColorInfo, 'color_name') }}</span>
              </div>
              <div class="color-item">
                <span class="label">商品名称：</span>
                <span class="value">{{ getFieldValue(conflictColorInfo, 'product_name') }}</span>
              </div>
              <div class="color-item">
                <span class="label">品牌：</span>
                <span class="value">{{ getFieldValue(conflictColorInfo, 'brand_name') }}</span>
              </div>
            </div>
          </div>
        </div>
        <div v-if="duplicateDimensions" class="duplicate-dimensions-info">
          <h4>重复的向量维度：</h4>
          <div class="dimensions-details">
            <div v-for="(value, key) in duplicateDimensions" :key="key" class="dimension-item">
              <span class="label">{{ getDimensionLabel(key) }}：</span>
              <span class="value">{{ Array.isArray(value) ? value.join(', ') : value }}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="duplicate-dialog-footer">
        <button class="btn btn-primary" @click="handleClose">我知道了</button>
      </div>
    </div>
  </div>
</template>

<script>
import { getFullImageUrl } from '../utils/imageUtils'
import { getAngleDisplayName } from '../utils/angleUtils'

export default {
  name: 'DuplicateCheckDialog',
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    conflictColorInfo: {
      type: Object,
      default: null,
    },
    duplicateDimensions: {
      type: Object,
      default: null,
    },
  },
  emits: ['close'],
  setup(props, { emit }) {
    // 安全地获取字段值（适配新旧数据结构）
    const getFieldValue = (obj, fieldName) => {
      if (!obj || !obj[fieldName]) {
        return ''
      }
      const field = obj[fieldName]
      if (typeof field === 'object' && 'value' in field) {
        return field.value || ''
      }
      return field || ''
    }

    // 获取维度标签名称
    const getDimensionLabel = key => {
      const labelMap = {
        clothingType: '服装类型',
        color: '颜色',
        pattern: '图案',
        material: '材质',
        sleeveType: '袖型',
        collarType: '领型',
      }
      return labelMap[key] || key
    }

    // 获取冲突颜色的图片
    const getConflictImages = colorInfo => {
      if (!colorInfo || !colorInfo.images) {
        return []
      }

      // 确保 images 是数组
      const images = Array.isArray(colorInfo.images) ? colorInfo.images : []

      // 返回前4张图片，避免弹窗过大
      return images.slice(0, 4)
    }

    // 关闭弹窗
    const handleClose = () => {
      emit('close')
    }

    return {
      getFieldValue,
      getDimensionLabel,
      getConflictImages,
      getFullImageUrl,
      getAngleDisplayName,
      handleClose,
    }
  },
}
</script>

<style scoped>
/* 重复检查弹窗样式 */
.duplicate-dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.duplicate-dialog {
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  max-width: 500px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

.duplicate-dialog-header {
  padding: 20px 20px 0 20px;
  border-bottom: 1px solid var(--border-color);
}

.duplicate-dialog-header h3 {
  margin: 0;
  color: var(--error-color);
  font-size: 18px;
  font-weight: 600;
}

.duplicate-dialog-content {
  padding: 20px;
}

.duplicate-warning {
  margin-bottom: 20px;
  padding: 12px;
  background: #fff2f0;
  border: 1px solid #ffccc7;
  border-radius: 6px;
}

.duplicate-warning p {
  margin: 0;
  color: #cf1322;
  font-size: 14px;
  font-weight: 500;
}

.duplicate-color-info h4 {
  margin: 0 0 12px 0;
  color: var(--text-color);
  font-size: 16px;
  font-weight: 600;
}

.color-info-container {
  display: flex;
  gap: 20px;
  align-items: flex-start;
}

.color-images {
  flex-shrink: 0;
  width: 200px;
}

.color-images h5 {
  margin: 0 0 8px 0;
  color: var(--text-color);
  font-size: 14px;
  font-weight: 600;
}

.conflict-image-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.conflict-image-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.conflict-image {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid var(--border-color);
}

.conflict-image-angle {
  font-size: 11px;
  color: var(--text-secondary);
  font-weight: 500;
}

.color-details {
  flex: 1;
  background: #f8f9fa;
  border-radius: 6px;
  padding: 16px;
}

.color-item {
  display: flex;
  margin-bottom: 8px;
  align-items: center;
}

.color-item:last-child {
  margin-bottom: 0;
}

.color-item .label {
  min-width: 80px;
  font-weight: 500;
  color: var(--text-secondary);
  font-size: 14px;
}

.color-item .value {
  flex: 1;
  color: var(--text-color);
  font-size: 14px;
  word-break: break-all;
}

.duplicate-dimensions-info {
  margin-top: 20px;
}

.duplicate-dimensions-info h4 {
  margin: 0 0 12px 0;
  color: var(--text-color);
  font-size: 16px;
  font-weight: 600;
}

.dimensions-details {
  background: #fff7e6;
  border: 1px solid #ffd591;
  border-radius: 6px;
  padding: 16px;
}

.dimension-item {
  display: flex;
  margin-bottom: 8px;
  align-items: center;
}

.dimension-item:last-child {
  margin-bottom: 0;
}

.dimension-item .label {
  min-width: 80px;
  font-weight: 500;
  color: var(--text-secondary);
  font-size: 14px;
}

.dimension-item .value {
  flex: 1;
  color: #d46b08;
  font-size: 14px;
  font-weight: 500;
  word-break: break-all;
}

.duplicate-dialog-footer {
  padding: 0 20px 20px 20px;
  display: flex;
  justify-content: flex-end;
}

.duplicate-dialog-footer .btn {
  min-width: 100px;
}

/* 响应式设计 */
@media (max-width: 600px) {
  .color-info-container {
    flex-direction: column;
    gap: 16px;
  }

  .color-images {
    width: 100%;
  }

  .conflict-image-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .conflict-image {
    width: 60px;
    height: 60px;
  }
}

@media (max-width: 480px) {
  .conflict-image-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .conflict-image {
    width: 70px;
    height: 70px;
  }
}
</style>
